local guzheng = fk.CreateSkill {
    name = "SP_heg__guzheng",
}

Fk:loadTranslationTable {
    ["SP_heg__guzheng"] = "锢政",
    [":SP_heg__guzheng"] = "每回合每种牌名限一次，当你需要使用一张基本牌时，你可以横置当前回合角色，视为使用之，然后若你的手牌数：" ..
    "大于X，你重铸其的一张牌；等于X，对一名未处于横置状态的角色造成1点伤害；小于X，移除此武将牌并摸X张牌(X为场上的横置角色数)。",

    ["#SP_heg__guzheng"] = "锢政：横置当前回合角色视为使用一张基本牌",
    ["#SP_heg__guzheng_damage"] = "锢政：选择一名未处于横置状态的角色，对其造成1点伤害",
    ["@SP_heg__guzheng-turn"] = "锢政",
    ["#SP_heg__guzheng_recast"] = "锢政：选择 %src 的一张牌重铸",

    ["$SP_heg__guzheng1"] = "吾不为陛下分忧，谁为陛下分忧？",
    ["$SP_heg__guzheng2"] = "臣即率兵马、征伐曹魏。",
}

local H = require "packages.ol_hegemony.util"

guzheng:addEffect("viewas", {
    pattern = "slash,jink,peach,analeptic",
    prompt = "#SP_heg__guzheng",
    interaction = function(self, player)
        local all_names = Fk:getAllCardNames("b")
        local names = player:getViewAsCardNames(guzheng.name, all_names, {}, player:getTableMark("@SP_heg__guzheng-turn"))
        if #names == 0 then return end
        return UI.CardNameBox { choices = names, all_choices = all_names }
    end,
    filter_pattern = {
        min_num = 0,
        max_num = 0,
        pattern = "",
        subcards = {}
    },
    card_filter = Util.FalseFunc,
    before_use = function(self, player, use)
        local room = player.room
        room:addTableMark(player, "@SP_heg__guzheng-turn", use.card.trueName)
        local current = room.current
        current:setChainState(true)
    end,
    view_as = function(self, player, cards)
        if not self.interaction.data then return nil end
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = guzheng.name
        return card
    end,
    enabled_at_play = function(self, player)
        local current = Fk:currentRoom().current
        return not current.chained
    end,
    enabled_at_response = function (self, player, response)
      return not response and not Fk:currentRoom().current.chained
    end,
})

guzheng:addEffect(fk.CardUsing, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and table.contains(data.card.skillNames, guzheng.name) and player:hasSkill(guzheng.name) and
        not player.dead
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local num = player:getHandcardNum()
        local targets = table.filter(room.alive_players, function(p) return p.chained end)
        if num > #targets then
            local current = room.current
            if not current:isNude() then
                local card = room:askToChooseCard(player,{
                    target = current,
                    flag = "he",
                    prompt = "#SP_heg__guzheng_recast:"..current.id,
                    skill_name = guzheng.name,
                })
                if card then
                    room:recastCard({card}, current, guzheng.name)
                end
            end
        elseif num == #targets then
            local targetss = table.filter(room.alive_players, function(p) return not p.chained end)
            if #targetss == 0 then return end
            local to = room:askToChoosePlayers(player, {
                targets = targetss,
                min_num = 1,
                max_num = 1,
                prompt = "#SP_heg__guzheng_damage",
                skill_name = guzheng.name,
                cancelable = false,
            })
            if #to > 0 then
                room:damage({
                    from = player,
                    to = to[1],
                    skillName = guzheng.name,
                    damage = 1,
                })
            end
        elseif num < #targets then
            local isDeputy = H.inGeneralSkills(player, guzheng.name)
            if isDeputy then
                H.removeGeneral(player, isDeputy == "d")
            end
            player:drawCards(#targets, guzheng.name)
        end
    end,
})


return guzheng
